package com.ffcs.cn.action.map;

import com.ffcs.cn.DevelopProperties;
import com.ffcs.cn.action.common.AbstractPanelForm;
import com.ffcs.cn.action.common.FormModel;
import com.supermap.data.Dataset;
import com.supermap.desktop.controls.ui.controls.datasetChoose.DatasetChooseMode;
import com.supermap.desktop.controls.ui.controls.datasetChoose.SmDatasetChooser;
import com.supermap.desktop.controls.utilities.MapViewUIUtilities;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IBaseItem;
import com.supermap.desktop.core.Interface.IFormMap;
import com.supermap.desktop.core.enums.WindowType;
import com.supermap.desktop.core.implement.CtrlAction;
import com.supermap.desktop.controls.ui.controls.DialogResult;
import com.supermap.desktop.core.utilties.FormUtilities;
import com.supermap.desktop.core.utilties.TableUtilities;

import java.awt.event.ActionListener;


/**
 * 选择数据集添加到地图
 */
public class CtrlActionDatasetAddToNewMap extends CtrlAction {
    public CtrlActionDatasetAddToNewMap(IBaseItem caller) {
        super(caller);
    }

    @Override
    protected void run() {
        new DialogCtrlActionDatasetAddToNewMap().showDialog();
    }

    private static class DialogCtrlActionDatasetAddToNewMap extends AbstractPanelForm {
        public DialogCtrlActionDatasetAddToNewMap() {
            setTitle(DevelopProperties.getString("String_Title_DatasetAddToNewMap"));
        }

        @Override
        public FormModel getTableModel() {
            return new FormModel() {
                private final String[] columnNames = {
                        DevelopProperties.getString("String_DatasetName"),
                        DevelopProperties.getString("String_DatasourceName"),
                        DevelopProperties.getString("String_DatasetType")};

                @Override
                public Object getValueAt(int row, int column) {
                    Object bean = tableModel.get(row);
                    switch (column) {
                        case 0:
                            return bean;
                        case 1:
                            return ((Dataset) bean).getDatasource();
                        case 2:
                            return ((Dataset) bean).getType();
                    }
                    return null;
                }

                @Override
                public int getColumnCount() {
                    return columnNames.length;
                }

                @Override
                public String getColumnName(int column) {
                    return columnNames[column];
                }

                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return Dataset.class;
                }
            };
        }

        /**
         * 点击添加按钮，从数据集选择界面选择待添加到地图的数据集
         *
         * @return 点击添加按钮后触发事件的监听器
         */
        @Override
        public ActionListener getAddActionListener() {
            return e -> {
                SmDatasetChooser smDatasetChooser = new SmDatasetChooser(DatasetChooseMode.DATASET);
                if (smDatasetChooser.showDialog() == DialogResult.OK) {
                    java.util.List<Object> selectedObjects = smDatasetChooser.getSelectedObjects();
                    for (Object obj : selectedObjects) {
                        if (obj instanceof Dataset && !tableModel.contains(obj)) {
                            int rowCount = tableModel.getRowCount();
                            tableModel.add(obj);
                            TableUtilities.setTableSelectedRows(getItemTable(), rowCount);
                        }
                    }
                }
                smDatasetChooser.dispose();
            };
        }

        /**
         * 点击确定按钮，打开一张空白地图并将选中数据集添加进地图
         *
         * @return 点击确认按钮后触发事件的监听器
         */
        @Override
        public ActionListener getOkActionListener() {
            return e -> {
                try {
                    // 获取选择的数据集
                    Object[] data = tableModel.getData().toArray();
                    // 遍历数组并将Object数组转换成Dataset类型数组
                    Dataset[] datasets = new Dataset[data.length];
                    for (int i = 0; i < data.length; i++) {
                        if (data[i] instanceof Dataset) {
                            datasets[i] = (Dataset) data[i];
                        }
                    }
                    // 打开一个空白地图窗口
                    IFormMap formMap = (IFormMap) FormUtilities.fireNewWindowEvent(WindowType.MAP);
                    // 将选择的数据集添加到地图里
                    MapViewUIUtilities.addDatasetsToMap(formMap.getMapControl().getMap(), 0, datasets);
                } catch (Exception exception) {
                    Application.getActiveApplication().getOutput().output(exception);
                } finally {
                    this.dispose();
                }
            };
        }
    }


}

